package com.xc.freeapp.account.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.CheckUtils;
import com.xc.freeapp.common.ResponsePage;
import com.xc.freeapp.entity.Issue;
import com.xc.freeapp.entity.Timingvisit;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.TimingvisitService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.JsonUtil;
import com.xc.freeapp.util.TokenAnnotation;
@Controller
@RequestMapping("/v2/timingvisit")
@Api(value ="/v2/timingvisit",description = "定时复诊")
public class TimingVisitControllerV2 extends AbstractController {
	@Autowired
	private TimingvisitService timingvisitservice;
	/**
	 * 获取定时复诊列表
	 * 
	 * @throws BaseException
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取定时复诊列表")
	public ResponsePage<List<Timingvisit>> getTimingVisitList(
			@RequestParam(value = "page", required = false, defaultValue="0") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue="15") Integer limit)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		PageBounds pageBounds = new PageBounds(page, limit);
		List<Timingvisit> timeingvisits = timingvisitservice
				.gettimingvisit(authInfo.getUserId(),pageBounds);
		Paginator paginator =((PageList)timeingvisits).getPaginator();
		return new ResponsePage<List<Timingvisit>>(timeingvisits, paginator);

	}

	/**
	 * 保存定时复诊
	 * 
	 * @param datamap
	 * @return
	 * @throws BaseException
	 */
//	@TokenAnnotation(required=true)
//	@RequestMapping(value = "", method = RequestMethod.POST)
//	@ResponseBody
//	@ApiOperation(value = "保存定时复诊")
	public Timingvisit saveTimingVisit(@RequestBody Timingvisit timingvisit)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		CheckUtils.notNull(Constant.VER_NOTNULL_BYNAME, timingvisit);
		timingvisit.setUserid(authInfo.getUserIntId());
		timingvisitservice.savetimingvisit(timingvisit);
		Timingvisit responsetimingvisit = timingvisitservice
				.gettiming(timingvisit.getId());
		return responsetimingvisit;
	}
	/**
	 * 更新定时复诊
	 * 
	 * @param datamap
	 * @return
	 * @throws BaseException
	 */
//	@TokenAnnotation(required=true)
//	@RequestMapping(value = "", method = RequestMethod.PUT)
//	@ResponseBody
//	@ApiOperation(value = "更新定时复诊")
	public Timingvisit updateTimingVisit(@RequestBody Timingvisit timingvisit)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		CheckUtils.notNull(Constant.VER_NOTNULL_BYNAME, timingvisit);
		timingvisit.setUserid(authInfo.getUserIntId());
		timingvisitservice.updatetimingvisit(timingvisit);
		Timingvisit responsetimingvisit = timingvisitservice
				.gettiming(timingvisit.getId());
		return responsetimingvisit;
	}
//	@TokenAnnotation(required=true)
//	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
//	@ResponseBody
	public Timingvisit delTimingVisit(@PathVariable("id") Integer id) throws BaseException {
	
		Timingvisit timingvisit = new Timingvisit();
		timingvisit.setId(id);
		timingvisit.setDelflag('d');
		timingvisitservice.updatetimingvisit(timingvisit);
		Timingvisit timingvisits = timingvisitservice.gettiming(timingvisit
				.getId());
		return timingvisits;
	}

}
